{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字符串和列表的问题...\n",
    "在这个 notebook 中，当我们试图首先用字符串表示票，然后再用列表表示时，会遇到一些问题。\n",
    "\n",
    "## 我们需要跟踪哪些数据？\n",
    "目前，单个`ticket`需要有三个与之相关的数据：\n",
    "\n",
    "*  日期，例如`\"2018-12-31\"`。\n",
    "\n",
    "*  优先级： `\"high\"`、 `\"medium\"` 或`\"low\"`。\n",
    "\n",
    "*  描述。\n",
    "\n",
    "### 尝试1 - 将票表示为字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Option 1 - represent a ticket as a string where each \n",
    "# field is separated by a newline \\n character.\n",
    "def create_ticket(date, priority, description):\n",
    "    ticket = date + \"\\n\"\n",
    "    ticket = ticket + priority + \"\\n\"\n",
    "    ticket = ticket + description + \"\\n\"\n",
    "    return ticket"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "ticket_1 = create_ticket(\"2018-12-31\", \"low\", \"example description\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-12-31\n",
      "low\n",
      "example description\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(ticket_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's see how easy it is to do a common task using this\n",
    "# string representation of a ticket. The following function\n",
    "# retrieves a \"description\" from a ticket.\n",
    "def get_description(ticket):\n",
    "    \n",
    "    # 1. separate string into individual lines\n",
    "    lines = ticket.splitlines()\n",
    "    \n",
    "    # 2. get the last line (which has the description)\n",
    "    description = lines[-1]\n",
    "    \n",
    "    return description"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "example description\n"
     ]
    }
   ],
   "source": [
    "description_1 = get_description(ticket_1)\n",
    "print(description_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 它似乎运行良好......对吗？\n",
    "到目前为止，似乎字符串用起来也很好。但是，打破这个体系也是很容易的，并不需要太多的“边缘情况”......"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "date = \"2018-12-29\"\n",
    "priority = \"high\"\n",
    "description = \"\"\"Vehicle did not slow down when \n",
    "SLOW\n",
    "SCHOOL\n",
    "ZONE\n",
    "was written on road.\"\"\"\n",
    "\n",
    "ticket_2 = create_ticket(date, priority, description)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "was written on road.\n"
     ]
    }
   ],
   "source": [
    "# what do you think will happen when we try to retrieve a\n",
    "# description from this ticket using our get_description function?\n",
    "description_2 = get_description(ticket_2)\n",
    "print(description_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 让我们来修复它吧！\n",
    "\n",
    "当然...你可以用一个`get_description`函数来解决这个问题。但是，这需要更多的代码，阅读起来会比较困难，并且不能解决根本问题：\n",
    "\n",
    "> **字符串不是用来表示票的正确数据结构。**即使一张票只是一堆文本，但该文本具有*结构*，并且如果用一个字符串表示票，将忽略该结构。\n",
    "\n",
    "---------\n",
    "\n",
    "## 尝试2 - 将票表示为列表\n",
    "相对于将票表示为字符串的代码，下面的代码更加简洁，也更能避开某些问题。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Option 2 - Use a list to represent a ticket\n",
    "def create_ticket(date, priority, description):\n",
    "    return [date, priority, description]\n",
    "\n",
    "def get_description(ticket):\n",
    "    return ticket[-1]\n",
    "\n",
    "date = \"2018-12-29\"\n",
    "priority = \"high\"\n",
    "description = \"\"\"Vehicle did not slow down when \n",
    "SLOW\n",
    "SCHOOL\n",
    "ZONE\n",
    "was written on road.\"\"\"\n",
    "\n",
    "ticket_2 = create_ticket(date, priority, description)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# what do you think will happen when we try to retrieve a\n",
    "# description from this ticket using our NEW get_description function?\n",
    "description_2 = get_description(ticket_2)\n",
    "print(description_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 那么，存在什么问题呢？\n",
    "当一张票只有这三个字段时，这个列表并**不是**一个很好的选择。但出于可读性的原因，如果我们可以编写这样的代码，也是很不错的："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "priority =    ticket['priority']\n",
    "date =        ticket['date']\n",
    "description = ticket['description']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "而不是这样的代码："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "priority =    ticket[1]\n",
    "date =        ticket[0]\n",
    "description = ticket[2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "至于为什么列表不是最优的表示方法（将在后面进行探讨），还有一些其他的原因，但现在让我们专注于可读性方面的改进。\n",
    "\n",
    "在下一节中，你将学习更多关于Python`dictionary`的知识。以下代码显示了它们是如何工作的："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dictionary_ticket = {\"date\" : \"2018-12-28\",\n",
    "                     \"priority\" : \"low\",\n",
    "                     \"description\" : \"car drove too slow\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(dictionary_ticket['description'])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
